//index.js
//获取应用实例
const app = getApp()
var indexData = require("../indexdata/indexdata.js");
var num = 0;
var sortRule1 = true; // 价格
var sortRule2 = true; // 销量
var sortClick1=2;
var sortClick2=2;
var twoBtn=true;//单列多列
Page({
  data: {
    bannerImg: [//轮播图片
      "../images/index/banner.png",
      "../images/index/banner.png",
      "../images/index/banner.png"
    ],    
    classif: [//分类
      {
        "img": "../images/index/class1.png",
        "id": "1",
        "text": "巴氏鲜奶"
      },
      {
        "img": "../images/index/class2.png",
        "id": "2",
        "text": "酸奶"
      },
      {
        "img": "../images/index/class3.png",
        "id": "3",
        "text": "发酵乳"
      },
      {
        "img": "../images/index/class4.png",
        "id": "4",
        "text": "其他商品"
      }
      ],
    flashSalebg: {
      "flashSalebg1": "http://www.tianshangding.cn/font/img/countDownR1.png",
      "flashSalebg2": "http://www.tianshangding.cn/font/img/countDownR2.png",
      "flashSalebg3": "http://www.tianshangding.cn/font/img/countDownR3.png"
    },
    sortClick1:1,
    sortClick2: 1,
    twoBtn:true,
    sortImg:{
      "sort1":"../images/index/sort1.png",
      "sort2": "../images/index/sort2.png",
      "sort3": "../images/index/sort3.png",
      "two": "../images/index/two.png",//两列
      "one": "../images/index/one.png"//单行
    },
    usedata: "",//产品列表信息
    usedata2: "",//产品列表信息
    shopSumText: "",
    timer:"",//定时器
    countDownTime: {// 倒计时
      "dianImg":"../images/index/dian.png",//时间点
      "startTime": "November 10,2019 23:59:50",//开始日期
      "endTime": "November 11,2019 00:00:00",//结束日期
      "hour":"00",//时
      "minute": "00",//分
      "second": "10"//秒
    },
    navbarInitTop: 0, //导航栏初始化距顶部的距离
    isFixedTop: false, //是否固定顶部
    flag:false,
    shoppingNumber:'3',
  },
  onLoad: function () {
    this.setData({
      //推荐消息， 订奶列表数据
      usedata: indexData.setdata,
      messageRolling: indexData.messageRolling
    })
    wx.setTabBarBadge({//购物车数量
      index: 1,
      text: this.data.shoppingNumber,
      color: "black"
    })
  },
  /**
 * 生命周期函数--监听页面初次渲染完成
 */
  onReady: function () {

  },
  
  // 页面渲染 调用
  onShow: function () {
    // new Date('2017-06-06 15:31:09').getTime()
    this.countDown();
    var that = this;

    if (that.data.navbarInitTop == 0) {
      //获取节点距离顶部的距离
      wx.createSelectorQuery().select('#navbar').boundingClientRect(function (rect) {
        if (rect && rect.top > 0) {
          var navbarInitTop = parseInt(rect.top);
          that.setData({
            navbarInitTop: navbarInitTop
          });
        }
      }).exec();
    }
  },
  /**
 * 监听页面滑动事件
 */
  onPageScroll: function (e) {
    var that = this;
    var scrollTop = parseInt(e.scrollTop); //滚动条距离顶部高度
    //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    var isSatisfy = scrollTop >= that.data.navbarInitTop ? true : false;
    //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    if (that.data.isFixedTop === isSatisfy) {
      return false;
    }
    that.setData({
      isFixedTop: isSatisfy,
      flag: isSatisfy
    });
  },
  countDown: function () {//倒计时
    let that = this;
    var startTime = new Date(this.data.countDownTime.startTime).getTime();
    var endTime = new Date(this.data.countDownTime.endTime).getTime();
    var totalSecond = (endTime - startTime)/1000;
    var t = totalSecond;
    that.setData({
    timer: setInterval(function () {
      t--;
      if (t > 0) {
        that.setData({
          hour:addzero(Math.floor(t / 3600)),
          minute:addzero(Math.floor(t % 3600 / 60)),
          second: addzero(Math.floor(t % 60))
        })
      } else {
        that.setData({
          hour: "00",
          minute:"00",
          second:"00"
        })
        clearInterval(that.data.timer);
      }
    }, 1000)

    })
    function addzero(n) {
      if (n < 10) {
        return '0' + n
      } else {
        return '' + n
      }
    }
  },
  shopBtn: function () {
    // 购物车添加
    num++;
    this.setData({
      shopSumText: num
    })
    app.globalData.ee = num;
  },
  mySort1: function (e) {
    // 价格
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule2 = true;
    sortClick2 = 1;
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2=2;
    if (sortClick1 == 1){
      sortRule1 =true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
      sortRule1 = true;
    } else if (sortClick1 == 2){
      sortRule1=true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });  
    } else if (sortClick1 == 3){
      sortRule1 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule1))
      });
    }
    this.setData({
      sortClick1:sortClick1
    })
    console.log(sortClick1)
    sortClick1++;
    if (sortClick1 > 3) {
      sortClick1 = 1;
    }
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop;//isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
        wx.pageScrollTo({
          scrollTop: _heightd,   //页面滚动的距离
          duration: 300,    //页面滚动速度 单位ms
          success: function (e) {    //成功函数
            that.setData({
              flag: false
            })
          }
        });
    }
  },
  mySort2: function (e) {
    // 销量
    //property 根据什么排序
    var property = e.currentTarget.dataset.property;
    var self = this;
    var arr = self.data.usedata;
    sortRule1 = true;
    sortClick1 = 1;
    this.setData({
      sortClick1: sortClick1
    })
    sortClick1=2;
    if (sortClick2 == 1) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
      sortRule2 = true;
    } else if (sortClick2 == 2) {
      sortRule2 = false;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    } else if (sortClick2 == 3) {
      sortRule2 = true;
      self.setData({
        usedata: arr.sort(self.compare(property, sortRule2))
      });
    }
    this.setData({
      sortClick2: sortClick2
    })
    sortClick2++;
    if (sortClick2 > 3) {
      sortClick2 = 1;
    }
    console.log(sortClick2)
    // 点击排序
    var that = this;
    var isFixedTop = that.data.isFixedTop;//isFixedTop是一个开关变量，用来控制点击滑动到指定位置
    var query = wx.createSelectorQuery(); //获取某个元素返回的是一个对象
    if (isFixedTop) {
      var _heightd = this.data.navbarInitTop;
      wx.pageScrollTo({
        scrollTop: _heightd,   //页面滚动的距离
        duration: 300,    //页面滚动速度 单位ms
        success: function (e) {    //成功函数
          that.setData({
            flag: false
          })
        }
      });
    }

  },
  compare: function (property, bol) {
    // 排序计算
    return function (a, b) {
      var value1 = a[property];
      var value2 = b[property];
      if (bol) {
        return value1 - value2;
      } else {
        return value2 - value1;
      }
    }
    
  },
  twoBtn:function(){
    twoBtn = !twoBtn;
    this.setData({
      twoBtn: twoBtn
    })
  },
  goClassif: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../indexDetails/indexDetails?newsid=' + newsid
      // 跳转到其他分类
    })
  },
  goDetails: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.navigateTo({
      url: '../details/details?newsid=' + newsid
      // 跳转到商品详情
    })
  },
  //转发
  onShareAppMessage: function () {
    let users = wx.getStorageSync('user');
    if (res.from === 'button') { }
    return {
      title: '转发',
      path: '/pages/index/index',
      success: function (res) { }
    }
  },
  goRegion: function () {
    wx.navigateTo({
      url: '../region/region' 
      // 20200211跳转到地区选择
    })
  },
  onShareAppMessage: function () {
    return {
      title: '弹出分享时显示的分享标题',
      desc: '分享页面的内容',
      path: '/pages/index/index' // 路径，传递参数到指定页面。
    }
  }
})
